<script setup>
  // 导入
  import { useTodoStore } from '@/stores/todo'
import { computed } from 'vue';
  // 使用
  const todoStore = useTodoStore()

  // 设置计算属性 -> 是否全选
  // 由于 stores 的计算属性不能赋值 所以要在这里重新设置一个新的计算属性
  const isAllSelect = computed({
    get() {
      return todoStore.isAll
    },
    set(flag) {
      todoStore.checkAll(flag)
    }
  })
</script>

<template>
  <section class="main">
    <input
      id="toggle-all"
      class="toggle-all"
      type="checkbox"
      v-model="isAllSelect"
    />
    <label for="toggle-all">Mark all as complete</label>
    <ul class="todo-list">
      <li
        v-for="item, index in todoStore.showTodos"
        :key="item.id"
        :class="{ completed: item.finished }"
      >
        <div class="view">
          <input
            class="toggle"
            type="checkbox"
            v-model="item.finished"
          />
          <label>{{ item.text }}</label>
          <button
            class="destroy"
            @click="todoStore.delTodo(index)"
          ></button>
        </div>
      </li>
    </ul>
  </section>
</template>